<template>
  <div>
    <v-header :showback='false'>
      <div class="sc-input flex-center" @click.stop.prevent="goSearch">
        <input type="text" placeholder="Search" :value="key" readonly>
        <img class="ico-search" :src="sImg" alt="">
      </div>
      <div class="sc-cancel" @click.stop.prevent="goBack"><span>取消</span></div>
    </v-header>
    <div v-if="hasdata==0"></div>
    <div v-else-if="hasdata==1">
      <v-list :load-handle='reload' :more-handle='loadmore' :more-handling='isLoading' :isend='isEnd'>
        <shoe-unit v-for="item in list" :key="item.id" :unit="item"></shoe-unit>
      </v-list>
    </div>
    <no-shoe v-else>未搜索到鞋子信息</no-shoe>
  </div>
</template>
<script>
import ShoeUnit from '../../templates/ShoeListUnit'
import NoShoe from '../../templates/ShoeNodata'
import { StockXSearch } from '../../../common/api/stock'
import { ENUM } from '../../../common/util'
export default {
  components: { ShoeUnit, NoShoe },
  data() {
    return {
      hasdata: 0,
      sImg: '//files.eyee.com/mcdn/static/img/search_gray.png', // require('../../../assets/img/search_gray.png'),
      key: '',
      list: [],
      isEnd: false,
      isLoading: false,
      pagedata: { sumcount: 0, pagesize: 0, pageindex: 1 }
    }
  },
  mounted() {
    this.initShare({ type: 0 })
    let param = this.$route.params
    this.key = param && param.key
    // console.log('key:%s,param', this.key, param);
    this.reload()
  },
  methods: {
    goBack() {
      if (window.mqq) window.mqq.ui.popBack()
      else this.$router.back()
    },
    goSearch() {
      this.$router.replace('/search/' + ENUM.SearchType.newsShoe.val)
    },
    reload($el) {
      this.list = []
      this.pagedata.pageindex = 1
      this.hasdata = 0
      this.req()
    },
    loadmore($el) {
      if (this.isEnd === true) {
        this.toast('已无更多')
      } else {
        this.pagedata.pageindex += 1
        this.req()
      }
    },
    req() {
      this.isLoading = true
      let page = this.pagedata
      StockXSearch({ pageindex: page.pageindex, keyword: this.key }).then(res => {
        // console.log('StockXList result', res);
        if (res) {
          if (res.list && res.list.length > 0) {
            this.hasdata = 1
            if (res.pageindex == 1) this.list = res.list
            else this.list.push(...res.list)
          } else this.hasdata = 2
          if (res.pageindex > 0) page.pageindex = res.pageindex
          if (res.sumcount > -1) page.sumcount = res.sumcount
          if (res.pagesize > -1) page.pagesize = res.pagesize
          if (page.pageindex * page.pagesize >= page.sumcount) this.isEnd = true
        } else { this.hasdata = 2; this.toast('加载失败') }
        this.isLoading = false
      }).catch(e => { this.hasdata = 2; this.isLoading = false; this.toast(e && e.msg ? e.msg : ('error:' + e)) })
    }
  }
}
</script>
<style>

</style>
